import { sendGet, sendPost } from "./request.js";
import Vue from "vue";
let vm = new Vue();
import $router from '../router/index.js'

//注册新用户：
export let registerUser = d => sendPost('/api/register', d)

//用户登录：
export let userLogin = d => sendPost('/api/login', d)

//首页所有商品一级分类：
export let getFirstCate = () => sendGet('/api/getcate');

//首页轮播图数据:
export let getAdvimage = () => sendGet('/api/getbanner');

//首页推荐商品：
export let getRecgoods = () => sendGet('/api/gethortgoods');

//分类：获取商品的所有分类：
export let getAllCate = () => sendGet('/api/getcates');

//获取某个二级商品id获取其下面的商品数据
export let getSecondCateGoods = d => sendGet('/api/getgoodlist', d);

//商品详情：根据某个商品id查询商品详情数据：
export let getGoodsDetail = d => sendGet('/api/getgoodsinfo', d);

//获取用户id
export let getCurUserId = (curpath = '') => {
    let userinfoObj = localStorage['appUser'] ? JSON.parse(localStorage['appUser']) : '';
    if (userinfoObj == '') {
        let pathArr = ['/cart', '/mine'];
        if (pathArr.indexOf(curpath) !== -1) {
            vm.$notify({ type: 'danger', message: '请先登录' });
            $router.push('/login');
        } else {
            return '';
        }
    } else {
        return userinfoObj.uid;
    }
}

// 添加购物车
export let addCart = d => sendPost('/api/cartadd', d);

//修改购物车
export let editCart = d => sendPost('/api/cartedit', d);

//删除购物车
export let deleteCart = d => sendPost('/api/cartdelete', d);

// 获取购物车
export let getCart = d => sendGet('/api/cartlist', d);

//获取验证码
export let getCode = () => sendGet('/api/sms');

//token有效期检侧
export let checktoken = () => sendGet('/api/checktoken');

//查询地址
export let getAddress = (d) => sendGet('/api/addresslist',d);

//添加地址
export let addAddress = (d)=> sendPost ('/api/addressadd',d);

//编辑地址
export let editAddress = (d)=>sendPost('/api/addressedit',d);

//删除地址
export let removeAddress = (d)=>sendPost('/api/addressremove',d);